﻿using Abp.Dependency;
using Abp.Json;
using Abp.UI;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Castle.Core.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Magicodes.Sms.Aliyun
{
    public class AliyunSmsClient
    {
        private readonly IAliyunSmsSettting aliyunSmsSettting;
        public IAcsClient AcsClient { get; private set; }

        public AliyunSmsClient(IAliyunSmsSettting aliyunSmsSettting, ILogger logger)
        {
            if (aliyunSmsSettting == null)
            {
                throw new UserFriendlyException("请配置短信服务相关配置!");
            }
            try
            {
                logger.Debug("SmsSettings:" + aliyunSmsSettting.ToJsonString());
                this.aliyunSmsSettting = aliyunSmsSettting;
                var product = aliyunSmsSettting.Product;//短信API产品名称（短信产品名固定，无需修改）
                var domain = aliyunSmsSettting.Domain;//短信API产品域名（接口地址固定，无需修改）
                var accessKeyId = aliyunSmsSettting.AccessKeyId;//你的accessKeyId，参考本文档步骤2
                var accessKeySecret = aliyunSmsSettting.AccessKeySecret;//你的accessKeySecret，参考本文档步骤2

                var profile = DefaultProfile.GetProfile(aliyunSmsSettting.RegionId, accessKeyId, accessKeySecret);
                DefaultProfile.AddEndpoint(aliyunSmsSettting.EndpointName, aliyunSmsSettting.RegionId, product, domain);
                AcsClient = new DefaultAcsClient(profile);
                logger.Debug("已创建AcsClient。");
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message, ex);
                throw new UserFriendlyException("配置出错,请检查配置!");
            }
        }

    }
}
